import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/cacheHelper.dart';
import '../../model/inventory.dart';
import '../../pages/things/things_helper.dart';

import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/things.dart';

import '../../widgets/inputWidget.dart';
import '../../widgets/signatureCard2.dart';

import '../../helper/Helper.dart';

class ThingsInfoPage extends StatefulWidget {
  ThingsInfoPage(this.cb1);
  final Things cb1;

  @override
  ThingsInfoPageState createState() => new ThingsInfoPageState();
}

class ThingsInfoPageState extends State<ThingsInfoPage> {
  final String now = Helper.DateTimeNowString();

  bool ifLoad = false;
  late Things s1;
  late List<Inventory> RList;
  late double total;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    s1 = widget.cb1;
    var list1 = await Inventory.GetListByName(s1.TName);
    RList = list1;
    total = getTotal();

    setState(() {
      //RList=list1;
      ifLoad = true;
    });
  }

  double getTotal() {
    double x1 = 0;
    for (var a in RList) {
      x1 += a.Amount2;
    }
    return x1;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("物资记录"),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.refresh),
        onPressed: () => loadData(),
      ),
      body: ifLoad
          ? CleanInfoBody(context)
          : Center(
              child: SpinKitCircle(
              color: Colors.blue,
            )),
    );
  }

  CleanInfoBody(BuildContext context) => SingleChildScrollView(
        child: CleanInfoFields(context),
      );

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];

    list2.add(ThingsHelper.thingsCard3(
        s1.TName, s1.Unit, s1.TType, s1.Storage, total));

    for (var a in RList) {
      list4.add(ThingsHelper.InventoryCard2(context, a, () => {}));
      list4.add(InputWidget.getDivider());
    }

    list1.add(InputWidget.inCard2(list2));

    list1.add(InputWidget.inCard2(list4));

    return list1;
  }

  void onTapButton(BuildContext context, String xx) {
    if (xx == "审核签字") {
      //showPanel(context, signIt1, "审核签字");
    }
  }

  void showPanel(
      BuildContext context, SignaturedCallback checkIt, String title) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCard2Page(checkIt, title);
      },
    );
  }

  CleanInfoFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: getWidgetList(context),
        ),
      );
}
